//
//  DetailViewController.m
//  MartketNote
//
//  Created by Nguyen Minh Giang on 12/11/13.
//  Copyright (c) 2013 Nguyen Minh Giang. All rights reserved.
//

#import "DetailViewController.h"

@interface DetailViewController ()

@end

@implementation DetailViewController
@synthesize product = _product;

-(void)saveClick
{
    if([productText.text isEqualToString:@""])
    {
        UIAlertView *al = [[UIAlertView alloc] initWithTitle:@"Chu y" message:@"Ban chua nhap mat hang" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"Dong", nil];
        [al show];
    }
    else
    {
        if(_product)
        {
//            [ReadWriteDataClass updateProductWithID:_product newName:productText.text newDetail:detailText.text newStatus:NO];
        }
        else
        {
//            [ReadWriteDataClass insertNewProductWithID:[ReadWriteDataClass getProductID] name:productText.text detail:detailText.text status:NO];
        }
        [self.navigationController popViewControllerAnimated:YES];
//        [self dismissModalViewControllerAnimated:YES];
    }
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    
    self.title = @"New Note";
    UIBarButtonItem *btnSave = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(saveClick)];
    self.navigationItem.rightBarButtonItem = btnSave;
    
    float Y = self.navigationController.navigationBar.frame.size.height;
    
    //label product
    UILabel *lbProduct = [[UILabel alloc] initWithFrame:CGRectMake(10, Y + 10, 300, 30)];
    lbProduct.text = @"Mat hang";
    lbProduct.textAlignment = NSTextAlignmentLeft;
    lbProduct.font = [UIFont systemFontOfSize:16];
    lbProduct.textColor = [UIColor blackColor];
    lbProduct.backgroundColor = [UIColor clearColor];
    [self.view addSubview:lbProduct];
    
    //product text
    productText = [[UITextView alloc] initWithFrame:CGRectMake(lbProduct.frame.origin.x, lbProduct.frame.origin.y + lbProduct.frame.size.height + 6.0, lbProduct.frame.size.width, 80)];
    [productText.layer setCornerRadius:5.0];
    [productText.layer setBorderWidth:1.0];
    [productText.layer setBorderColor:[UIColor grayColor].CGColor];
    [productText.layer setMasksToBounds:YES];
    productText.font = [UIFont systemFontOfSize:16];
    productText.textColor = [UIColor blackColor];
    productText.delegate = self;
    [self.view addSubview:productText];
    
    //label detail
    UILabel *lbDetail = [[UILabel alloc] initWithFrame:CGRectMake(lbProduct.frame.origin.x, productText.frame.origin.y + productText.frame.size.height + 12, lbProduct.frame.size.width, lbProduct.frame.size.height)];
    lbDetail.text = @"Chi tiet";
    lbDetail.textColor = lbProduct.textColor;
    lbDetail.textAlignment = lbProduct.textAlignment;
    lbDetail.font = lbProduct.font;
    lbDetail.backgroundColor = lbProduct.backgroundColor;
    [self.view addSubview:lbDetail];
    
    //detail
    detailText = [[UITextView alloc] initWithFrame:CGRectMake(productText.frame.origin.x, lbDetail.frame.origin.y + lbDetail.frame.size.height + 6.0, lbDetail.frame.size.width, 120)];
    [detailText.layer setCornerRadius:productText.layer.cornerRadius];
    [detailText.layer setMasksToBounds:YES];
    [detailText.layer setBorderColor:productText.layer.borderColor];
    [detailText.layer setBorderWidth:productText.layer.borderWidth];
    detailText.font = productText.font;
    detailText.textColor = productText.textColor;
    detailText.delegate = self;
    [self.view addSubview:detailText];
    
    if(_product)
    {
        productText.text = [_product valueForKey:ATT_PRODUCT];
        detailText.text = [_product valueForKey:ATT_DETAIL];
    }
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [productText resignFirstResponder];
    [detailText resignFirstResponder];
}

#pragma mark - text view delegate
-(void)textViewDidBeginEditing:(UITextView *)textView
{
    if(textView == detailText)
    {
        self.view.frame = CGRectMake(self.view.frame.origin.x, - 100.0, self.view.frame.size.width, self.view.frame.size.height);
    }
}

-(void)textViewDidEndEditing:(UITextView *)textView
{
    self.view.frame = CGRectMake(self.view.frame.origin.x, 0.0, self.view.frame.size.width, self.view.frame.size.height);
}

@end
